home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Draw / Sources / EditCmds.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  10.0 KB  |  334 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                EditCmds.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef EDITCMDS_H
  15. #include "EditCmds.h"
  16. #endif
  17.  
  18. #ifndef DRAWSEL_H
  19. #include "DrawSel.h"
  20. #endif
  21.  
  22. #ifndef BASESHP_H
  23. #include "BaseShp.h"
  24. #endif
  25.  
  26. #ifndef BOUNDSHP_H
  27. #include "BoundShp.h"
  28. #endif
  29.  
  30. #ifndef LINESHP_H
  31. #include "LineShp.h"
  32. #endif
  33.  
  34. #ifndef OVALSHP_H
  35. #include "OvalShp.h"
  36. #endif
  37.  
  38. #ifndef RECTSHP_H
  39. #include "RectShp.h"
  40. #endif
  41.  
  42. #ifndef RRECTSHP_H
  43. #include "RRectShp.h"
  44. #endif
  45.  
  46. #ifndef TEXTSHP_H
  47. #include "TextShp.h"
  48. #endif
  49.  
  50. #ifndef DRAWPART_H
  51. #include "DrawPart.h"
  52. #endif
  53.  
  54. #ifndef DRAWFRM_H
  55. #include "DrawFrm.h"
  56. #endif
  57.  
  58. #ifndef DRAWLINK_H
  59. #include "DrawLink.h"
  60. #endif
  61.  
  62. // ----- OS Includes -----
  63.  
  64. #ifndef FWORDCOL_H
  65. #include "FWOrdCol.h"
  66. #endif
  67.  
  68. // ----- OpenDoc Includes -----
  69.  
  70. #ifndef SOM_Module_OpenDoc_Commands_defined
  71. #include <CmdDefs.xh>
  72. #endif
  73.  
  74. //========================================================================================
  75. // RunTime Info
  76. //========================================================================================
  77.  
  78. #ifdef FW_BUILD_MAC
  79. #pragma segment odfdrawcommand
  80. #endif
  81.  
  82. //========================================================================================
  83. // CDrawEditCommand
  84. //========================================================================================
  85.  
  86. //----------------------------------------------------------------------------------------
  87. //    CDrawEditCommand constructor
  88. //----------------------------------------------------------------------------------------
  89.  
  90. CDrawEditCommand::CDrawEditCommand (Environment *ev, 
  91.                                     ODCommandID commandID,
  92.                                     CDrawPart* part, 
  93.                                     CDrawFrame* frame, 
  94.                                     CDrawSelection* selection,
  95.                                     FW_Boolean canUndo) :
  96.     FW_CEditCommand(ev, commandID, frame, canUndo),
  97.         fDrawPart(part),
  98.         fDrawSelection(selection),
  99.         fShapeCollection(NULL),
  100.         fSavedLink(NULL)
  101. {
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. //    CDrawEditCommand destructor
  106. //----------------------------------------------------------------------------------------
  107.  
  108. CDrawEditCommand::~CDrawEditCommand()
  109. {
  110.     if (fShapeCollection)
  111.         delete fShapeCollection;
  112. }
  113.  
  114. //----------------------------------------------------------------------------------------
  115. //    CDrawEditCommand::DoPaste
  116. //----------------------------------------------------------------------------------------
  117. void CDrawEditCommand::DoPaste(Environment *ev)    // Override
  118. {
  119.     fDrawSelection->AdjustSelectionAfterPaste(ev, fFrame);
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    CDrawEditCommand::DoPasteAs
  124. //----------------------------------------------------------------------------------------
  125. void CDrawEditCommand::DoPasteAs(Environment *ev)    // Override
  126. {
  127.     // Select the newly-subscribed shapes
  128.     fSavedLink = GetDrawLinkManager(ev)->GetLatestSubscriber(ev);
  129.     fSavedLink->SelectShapes(ev);
  130.  
  131.     // Offset the selection, saving the offset for later updates
  132.     FW_CPoint offset = fDrawSelection->AdjustSelectionAfterPaste(ev, fFrame);
  133.  
  134.     // Save information about newly-created link
  135.     fSavedLink->SetUpdateOffset(ev, offset);
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. //    CDrawEditCommand::CommitUndone
  140. //----------------------------------------------------------------------------------------
  141. void CDrawEditCommand::CommitUndone(Environment* ev)    // Override
  142. {
  143.     if (fCommandID == kODCommandPasteAs)
  144.         GetDrawLinkManager(ev)->CommitUndone(ev, fSavedLink);
  145.     else
  146.         FW_CEditCommand::CommitUndone(ev);
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. //    CDrawEditCommand::SaveUndoState
  151. //----------------------------------------------------------------------------------------
  152. void CDrawEditCommand::SaveUndoState(Environment *ev)    // Override
  153. {
  154.     if ((fCommandID == kODCommandCut) || (fCommandID == kODCommandClear))
  155.     {
  156.         this->SaveShapeSelection(ev);
  157.     }
  158. }
  159.  
  160. //----------------------------------------------------------------------------------------
  161. //    CDrawEditCommand::SaveRedoState
  162. //----------------------------------------------------------------------------------------
  163. void CDrawEditCommand::SaveRedoState(Environment *ev)    // Override
  164. {
  165.     if (fCommandID == kODCommandPaste)
  166.     {
  167.         this->SaveShapeSelection(ev);
  168.     }    
  169.     else if (fCommandID == kODCommandPasteAs)
  170.     {
  171.         // Newly-created link has already been saved in DoPasteAs
  172.     }
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    CDrawEditCommand::FreeUndoState
  177. //----------------------------------------------------------------------------------------
  178. void CDrawEditCommand::FreeUndoState(Environment* ev)    // Override
  179. {
  180.     if (fCommandID == kODCommandCut || fCommandID == kODCommandClear)
  181.         this->DeleteSavedShapes(ev);
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. //    CDrawEditCommand::FreeRedoState
  186. //----------------------------------------------------------------------------------------
  187. void CDrawEditCommand::FreeRedoState(Environment* ev)    // Override
  188. {
  189.     if (fCommandID == kODCommandPaste)
  190.         this->DeleteSavedShapes(ev);
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. //    CDrawEditCommand::UndoIt
  195. //----------------------------------------------------------------------------------------
  196. void CDrawEditCommand::UndoIt(Environment *ev)    // Override
  197. {
  198.     FW_CEditCommand::UndoIt(ev);    // call inherited
  199.  
  200.     switch (fCommandID)
  201.     {
  202.         case kODCommandCut:
  203.         case kODCommandClear:
  204.             this->RestoreShapeSelection(ev);
  205.             break;
  206.  
  207.         case kODCommandPaste:
  208.             this->RemoveShapeSelection(ev);
  209.             break;
  210.  
  211.         case kODCommandPasteAs:
  212.             GetDrawLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
  213.             break;
  214.     }    
  215. }
  216.  
  217. //----------------------------------------------------------------------------------------
  218. //    CDrawEditCommand::RedoIt
  219. //----------------------------------------------------------------------------------------
  220. void CDrawEditCommand::RedoIt(Environment *ev)    // Override
  221. {
  222.     FW_CEditCommand::RedoIt(ev);    // call inherited
  223.  
  224.     switch (fCommandID)
  225.     {
  226.         case kODCommandCut:
  227.         case kODCommandClear:
  228.             this->RemoveShapeSelection(ev);
  229.             break;
  230.  
  231.         case kODCommandPaste:
  232.             this->RestoreShapeSelection(ev);
  233.             break;
  234.  
  235.         case kODCommandPasteAs:
  236.             GetDrawLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
  237.             break;
  238.     }    
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. //    CDrawEditCommand::RemoveShapeSelection
  243. //----------------------------------------------------------------------------------------
  244. void CDrawEditCommand::RemoveShapeSelection(Environment *ev)
  245. {
  246.     // First, empty the selection
  247.     fDrawSelection->CloseSelection(ev);
  248.  
  249.     // Select the saved shapes
  250.     this->SelectSavedShapes(ev);
  251.  
  252.     // Clear the selected shapes
  253.     fDrawSelection->ClearSelection(ev);
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. //    CDrawEditCommand::RestoreShapeSelection
  258. //----------------------------------------------------------------------------------------
  259. void CDrawEditCommand::RestoreShapeSelection(Environment *ev)
  260. {
  261.     // First, empty the selection
  262.     fDrawSelection->CloseSelection(ev);
  263.  
  264.     // Add the saved shapes back into the part
  265.     FW_COrderedCollectionIterator ite(fShapeCollection);
  266.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  267.     {
  268.         shape->RestoreShape(ev, fDrawPart);
  269.     }
  270.  
  271.     // Add the saved shapes to the selection
  272.     this->SelectSavedShapes(ev);
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. //    CDrawEditCommand::SaveShapeSelection
  277. //----------------------------------------------------------------------------------------
  278. void CDrawEditCommand::SaveShapeSelection(Environment *ev)
  279. {
  280.     // Save references to selected shapes
  281.     fShapeCollection = new FW_CPrivOrderedCollection;
  282.  
  283.     // Copy shape pointers from fDrawSelection
  284.     FW_COrderedCollectionIterator ite(fDrawSelection->GetSelectionCollection());
  285.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  286.     {
  287.         fShapeCollection->AddLast(shape);    
  288.     }
  289. }
  290.  
  291. //----------------------------------------------------------------------------------------
  292. //    CDrawEditCommand::SelectSavedShapes
  293. //----------------------------------------------------------------------------------------
  294. void CDrawEditCommand::SelectSavedShapes(Environment *ev)
  295. {
  296.     // Add the shapes in fShapeCollection to fDrawSelection
  297.     FW_COrderedCollectionIterator ite(fShapeCollection);
  298.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  299.     {
  300.         fDrawSelection->AddToSelection(ev, shape, FALSE);
  301.         fDrawSelection->RedrawShape(ev, shape);
  302.     }
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    CDrawEditCommand::DeleteSavedShapes
  307. //----------------------------------------------------------------------------------------
  308. void CDrawEditCommand::DeleteSavedShapes(Environment *ev)
  309. {
  310.     CBaseShape* shape;
  311.     
  312.     // before deleting the shape check if they are not promised
  313.     FW_COrderedCollectionIterator ite(fShapeCollection);
  314.     for (shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  315.     {
  316.         shape->CheckPromise(ev, fDrawPart);
  317.     }
  318.     
  319.     // ----- Delete all the shapes -----
  320.     while ((shape = (CBaseShape*)fShapeCollection->First()) != NULL)
  321.     {
  322.         fShapeCollection->Remove(shape);
  323.         delete shape;
  324.     }        
  325. }
  326.  
  327. //----------------------------------------------------------------------------------------
  328. //    CDrawEditCommand::GetDrawLinkManager
  329. //----------------------------------------------------------------------------------------
  330. CDrawLinkManager* CDrawEditCommand::GetDrawLinkManager(Environment* ev) const
  331. {
  332.     return (CDrawLinkManager*) fPart->GetLinkManager(ev);
  333. }
  334.